package com.hitqz.robot.biz.controller;

import cn.hutool.extra.spring.SpringUtil;
import com.google.common.cache.Cache;
import com.hitqz.robot.api.common.entity.Robot;
import com.hitqz.robot.api.dispatch.protocol.dto.RobotNextStepDto;
import com.hitqz.robot.biz.service.RobotService;
import com.hitqz.robot.common.core.util.R;
import io.swagger.v3.oas.annotations.Parameter;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author xupkun
 * @date 2024/5/20
 */
@RestController
@AllArgsConstructor
@RequestMapping("/log")
public class LogController {

    private final RobotService robotService;

    @GetMapping(value = "{logLevel}")
    public R getLog(@Parameter(name = "日志等级， 请求分别为 info/warn/error" ) @PathVariable String logLevel) {




        return R.ok();
    }



    @GetMapping(value = "clear")
    public R  clear() {
        Robot robot = robotService.getDefaultRobot();
        Cache<String, RobotNextStepDto> nextStep = SpringUtil.getBean("nextStep");
        RobotNextStepDto nextDto  = nextStep.getIfPresent(robot.getRobotSn());
        while (true) {
            if (nextDto.getPosCode() == "LM12") {
                break;
            }
            nextDto = nextStep.getIfPresent(robot.getRobotSn());
        }
        nextDto.setPosCode("-1");
        nextStep.put(robot.getRobotSn(),  nextDto);
        return R.ok();
    }



    @GetMapping(value = "set")
    public R  set() {
        Robot robot = robotService.getDefaultRobot();
        Cache<String, RobotNextStepDto> nextStep = SpringUtil.getBean("nextStep");
        RobotNextStepDto nextDto  = new RobotNextStepDto();
        nextDto.setPosCode("LM12");
        nextStep.put(robot.getRobotSn(),  nextDto);
        return R.ok();
    }



}
